﻿/*
 * Created by SharpDevelop.
 * User: ratul
 * Date: 3/5/2010
 * Time: 2:34 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Collections;
using System.Collections.Generic;
using H264Impl.NET.Coding;

namespace H264Impl.NET.NAL.NonVCLNALUnit
{
	/// <summary>
	/// Description of PictureParameterSet.
	/// </summary>
	public class PictureParameterSetNonVCLNALUnit : SequenceParameterSetNonVCLNALUnit
	{
		public PictureParameterSetNonVCLNALUnit(BitStream _NALUnitBitStream, bool forbiddenZeroBit, UInt32 _NALRefID) : base(_NALUnitBitStream, forbiddenZeroBit, _NALRefID)
		{
		}
		
		private void parseRBSP()
		{
			picparameterSetId = ExpGolomb.getUnsignedIntegerElement(RBSP);
			seqParameterSetId = ExpGolomb.getUnsignedIntegerElement(RBSP);
			entropyCodingModeFlag = RBSP.getBit();
			bottomFieldPicOrderInFramePresentFlag = RBSP.getBit();
			numSliceGroupsMinus1 = ExpGolomb.getUnsignedIntegerElement(RBSP);
			if( numSliceGroupsMinus1 > 0 ) {
				sliceGroupMapType = ExpGolomb.getUnsignedIntegerElement(RBSP);
				if( sliceGroupMapType == 0 )
				{
					runLengthMinus1 = new UInt32[numSliceGroupsMinus1+1];
					for( int iGroup = 0; iGroup <= numSliceGroupsMinus1; iGroup++ )
						runLengthMinus1[iGroup] = ExpGolomb.getUnsignedIntegerElement(RBSP);
				}
				else if( sliceGroupMapType == 2 )
				{
					topLeft = new UInt32[numSliceGroupsMinus1];
					bottomRight = new UInt32[numSliceGroupsMinus1];
					for( int iGroup = 0; iGroup < numSliceGroupsMinus1; iGroup++ ) {
						topLeft[iGroup] = ExpGolomb.getUnsignedIntegerElement(RBSP);
						bottomRight[iGroup] = ExpGolomb.getUnsignedIntegerElement(RBSP);
					}
				}
				else if( sliceGroupMapType == 3 || sliceGroupMapType == 4 || sliceGroupMapType == 5 )
				{
					sliceGroupChangeDirectionFlag = RBSP.getBit();
					sliceGroupChangeRateMinus1 = ExpGolomb.getUnsignedIntegerElement(RBSP);
				}
				else if( sliceGroupMapType == 6)
				{
					picSizeInMapUnitsMinus1 = ExpGolomb.getUnsignedIntegerElement(RBSP);
					sliceGroupId = new UInt32[picSizeInMapUnitsMinus1+1];
					for( int i = 0; i <= picSizeInMapUnitsMinus1; i++ )
						sliceGroupId[ i ] = RBSP.getBits((UInt16)(Math.Ceiling(Math.Log(numSliceGroupsMinus1+1, 2))));
				}
				
			}
			numRefIdxl0DefaultActiveMinus1 = ExpGolomb.getUnsignedIntegerElement(RBSP);
			numRefIdxl1DefaultActiveMinus1 = ExpGolomb.getUnsignedIntegerElement(RBSP);
			weightedPredFlag = RBSP.getBit();
			weightedBipredIdc = (byte)(RBSP.getBits(2));
			picInitQpMinus26 = ExpGolomb.getSignedIntegerElement(RBSP);
			picInitQsMinus26 = ExpGolomb.getSignedIntegerElement(RBSP);
			chromaQpIndexOffset = ExpGolomb.getSignedIntegerElement(RBSP);
			deblockingFilterControlPresentFlag = RBSP.getBit();
			constrainedIntraPredFlag = RBSP.getBit();
			redundantPicCntPresentFlag = RBSP.getBit();
			if(more_rbsp_data())
			{
				transform8x8ModeFlag = RBSP.getBit();
				picScalingMatrixPresentFlag = RBSP.getBit();
				if(picScalingMatrixPresentFlag)
				{
					picScalingListPresentFlag = new bool[6+((chromaFormatIdc != 3)?2:6)*Convert.ToUInt32(transform8x8ModeFlag)];
					for( int i = 0; i<6+((chromaFormatIdc != 3)?2:6)*Convert.ToUInt32(transform8x8ModeFlag); i++ )
					{
						picScalingListPresentFlag[i] = RBSP.getBit();
						if(picScalingListPresentFlag[i])
						{
							if(i<6)
							{
								//scalingList();
							}
							else
							{
								//scalingList();
							}
									
						}
					}
							
				}
				secondChromaQpIndexOffset = ExpGolomb.getSignedIntegerElement(RBSP);	
			}
			
		}
		
		bool more_rbsp_data()
		{
			return true;
		}
		UInt32 picparameterSetId;
		UInt32 seqParameterSetId;
		bool entropyCodingModeFlag;
		bool bottomFieldPicOrderInFramePresentFlag;
		UInt32 numSliceGroupsMinus1;
		UInt32 sliceGroupMapType;
		UInt32[] runLengthMinus1;
		UInt32[] topLeft;
		UInt32[] bottomRight;
		bool sliceGroupChangeDirectionFlag;
		UInt32 sliceGroupChangeRateMinus1;
		UInt32 picSizeInMapUnitsMinus1;
		UInt32[] sliceGroupId;
		UInt32 numRefIdxl0DefaultActiveMinus1;
		UInt32 numRefIdxl1DefaultActiveMinus1;
		bool weightedPredFlag;
		byte weightedBipredIdc;
		Int32 picInitQpMinus26;
		Int32 picInitQsMinus26;
		Int32 chromaQpIndexOffset;
		bool deblockingFilterControlPresentFlag;
		bool constrainedIntraPredFlag;
		bool redundantPicCntPresentFlag;
		bool transform8x8ModeFlag;
		bool picScalingMatrixPresentFlag;
		bool[] picScalingListPresentFlag;
		Int32 secondChromaQpIndexOffset;
	}
}
